<?php
namespace app\yunda\admin;

use app\common\builder\ZBuilder;
use app\yunda\model\Score_order;
use think\Db;

/**
 * Class Category大转盘中奖名单
 * @package app\yunda\admin
 */
class Prize extends Base{
    public function index()
    {
        // 获取查询和排序规则
        $map = $this->getMap();
        $map['yd_score_order.order_type']=2;//大转盘订单
        $map['yd_score_order.delete_time']=NULL;
        $order = $this->getOrder();
        $btn_1=[
            'title' => '确认兑奖',
            'icon'  => 'fa fa-fw fa-hand-o-up',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'data-title' => '确认要兑奖吗？',
            'href'  => url('edit_1', ['id' => '__id__'])
        ];
       // halt($map);
        $data = Db::view('yd_score_order')
            ->view('yd_user',['name','post_code','home','address','phone'],'yd_user.openid=yd_score_order.openid','left')
            ->view('yd_active','active_title','yd_active.id=yd_score_order.good_id','left')
            ->where($map)
            ->order($order)
            ->order('id desc')
            ->paginate()->each(function($item){
                $item['address1']=$item['home'].$item['address'];
                $arr=['否','是'];
                $item['is_pay']=$arr[$item['is_pay']];
                return $item;
            });
        return ZBuilder::make('table')
            ->setTableName('yd_score_order')  //需要修改 这里修改正确可以直接修改字段 删除
            ->addColumns([
                ['id','id'],
                ['name','收件人'],
                ['phone','收件人电话'],
                ['active_title','中奖的商品'],
                ['address1','收件地址'],
                ['post_code','邮编'],
                ['is_pay','是否兑换'],
                ['create_time','创建时间','datetime'],
                ['right_button', '操作', 'btn']
            ])
            //->addTopButton('add', [],[])
            ->addRightButton('edit_1',$btn_1)
            ->addTopButtons(['delete'])
            ->setRowList($data)
            ->addFilter(['name' => 'yd_user','active_title'=>'yd_active'])
            ->addFilter(['is_pay'],['否','是'])
            ->setSearch(['id' => 'ID'])
            ->fetch();
    }

    //确认兑奖
    public function edit_1($id=null)
    {
        if(empty($id))$this->error('参数错误');
        $row=Score_order::get($id);
        //halt($row);
        if($row['is_pay']==1)$this->error('该奖品已经兑换');
        Score_order::update(['id'=>$id,'is_pay'=>1]);
        $this->success('兑奖成功', null, '_parent_reload');
    }
}